/* HTTP GET Example using plain POSIX sockets

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
// #include "lwip/netdb.h"
// #include "lwip/dns.h"

/* Constants that aren't configurable in menuconfig */
#define WEB_SERVER "192.168.10.8"
#define WEB_PORT 8000
#define WEB_PORT_STR "8000"
#define WEB_PATH "/list/"

static const char *TAG = "http";

static const char *REQUEST = "GET " WEB_PATH " HTTP/1.1\r\n"
                             "Host: " WEB_SERVER ":" WEB_PORT_STR "\r\n"
                             "\r\n";


#define OTA_FW_PATH "/download/?v1.bin"
static const char *REQUEST_FW = "GET " OTA_FW_PATH " HTTP/1.1\r\n"
                             "Host: " WEB_SERVER ":" WEB_PORT_STR "\r\n"
                             "\r\n";


static void http_get_task(void *pvParameters)
{
  char recv_buf[2048];
  char host_ip[] = WEB_SERVER;
  struct sockaddr_in dest_addr;
  dest_addr.sin_addr.s_addr = inet_addr(host_ip);
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(WEB_PORT);
  int r;
  while (1)
  {
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    if (sock < 0)
    {
      vTaskDelay(1000 / portTICK_PERIOD_MS);
      continue;
    }
    ESP_LOGI(TAG, "... allocated sock");
    int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));

    if (err != 0)
    {
      ESP_LOGE(TAG, "... sock connect failed errno=%d", errno);
      close(sock);
      vTaskDelay(4000 / portTICK_PERIOD_MS);
      continue;
    }

    ESP_LOGI(TAG, "... connected");

    if (write(sock, REQUEST_FW, strlen(REQUEST_FW)) < 0)
    {
      ESP_LOGE(TAG, "... socket send failed");
      close(sock);
      vTaskDelay(4000 / portTICK_PERIOD_MS);
      continue;
    }
    ESP_LOGI(TAG, "... sock send success");

    struct timeval receiving_timeout;
    receiving_timeout.tv_sec = 5;
    receiving_timeout.tv_usec = 0;
    if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout,
                   sizeof(receiving_timeout)) < 0)
    {
      ESP_LOGE(TAG, "... failed to set sock receiving timeout");
      close(sock);
      vTaskDelay(4000 / portTICK_PERIOD_MS);
      continue;
    }
    ESP_LOGI(TAG, "... set socket receiving timeout success");

    /* Read HTTP response */
    do
    {
      bzero(recv_buf, sizeof(recv_buf));
      r = read(sock, recv_buf, sizeof(recv_buf) - 1);
      ESP_LOGE(TAG, "read from get: %d", r);
      // ESP_LOGI(TAG, "%s", (char *)recv_buf);
      // for (int i = 0; i < r; i++)
      // {
      //   putchar(recv_buf[i]);
      // }
    } while (r > 0);

    ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d.", r, errno);
    close(sock);
    for (int countdown = 10; countdown >= 0; countdown--)
    {
      ESP_LOGI(TAG, "%d... ", countdown);
      vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    ESP_LOGI(TAG, "Starting again!");
  }
}

void http_get_request(void)
{
  xTaskCreate(&http_get_task, "http_get_task", 10 * 1024, NULL, 5, NULL);
  }
